/*---------------------------------------------------
-----------------------------------------------------
Filename:	browser.m
Version:	3.3

Type:		maki
Date:		29. Aug. 2007 - 23:51 
Author:		Martin Poehlmann aka Deimos
E-Mail:		martin@skinconsortium.com
Internet:	www.skinconsortium.com
		www.martin.deimos.de.vu
-----------------------------------------------------
---------------------------------------------------*/

#define BROWSER_SCRIPT

#include <lib/std.mi>
#include <lib/config.mi>

#include attribs/init_windowpage.m
#include attribs/init_browser.m

#define FILE_NAME "browser.m"
//include lib/debug.m

Class PopupMenu AttribMenu;
Function AttribMenu createMenufromAttribList (List entries);
/**
	createMenufromAttribList() explanation
	the List "entries" MUST have the following style:
	item 0 - is a configItem, which represents the root item.
	all other items are ConfigAttributes, which will be autofilled
*/

Function loadInternalLinks();

Function string prepareWebString(string url, string replace);
Function string strReplace(string str, string replace, string by);
Function playListItem(int num);
Function enqueueListItem(int num);
Function performWebSearch (string searchstring);
Function BrowserReload();

Global List internal_LinksTitle, internal_LinksUrl, internal_LinksSubmenus;

Global Browser brw;
Global Button bback, bffwd, bhome, bbrowse, brefresh, bstop, bsearch, blinks;

Global Button scr_open, scr_close, scr_rescan, scr_play, scr_download;
Global Group scr_content, g_search, g_navurl, sg;
Global GuiList scr_list;
Global PopUpMenu scr_menu;
Global guiObject scr_open_bg;
Global Edit navurl, searchedit;

Global Layout normal, quicklink_name_layout;

Global Container quicklink_name;


System.onScriptLoaded ()
{
	initAttribs_windowpage();
	initAttribs_Browser();

	sg = getScriptGroup();
	normal = sg.getParentLayout();
	g_search = sg.getObject("browser.search");
	g_navurl = sg.getObject("browser.navurl");
	brw = sg.getObject("webbrowser");
	bback = sg.getObject("browser.back");
	bffwd = sg.getObject("browser.fwd");
	bhome = sg.getObject("browser.home");
	bbrowse = g_navurl.getObject("browser.navigate");
	brefresh = sg.getObject("browser.refresh");
	bstop = sg.getObject("browser.stop");
	navurl = g_navurl.getObject("browser.hedit");
	bsearch = g_search.getObject("search.choose");
	blinks = sg.getObject("browser.links");

	browser_search_attrib.onDataChanged();

	searchedit = g_search.getObject("search.edit");

	scr_open = sg.getObject("browser.scraper.show");
	scr_open_bg = sg.getObject("browser.scraper.show.bg");
	scr_content = sg.getObject("browser.scraper");
	scr_close = scr_content.getObject("browser.scraper.hide");
	scr_rescan= scr_content.getObject("browser.scraper.rescan");
	scr_play = scr_content.getObject("browser.scraper.play");
	scr_download = scr_content.getObject("browser.scraper.downlaod");
	scr_list = scr_content.getObject("scraper.results");

	navurl.setText(getPrivateString(getSkinName(), "Browser Home", "http://www.winamp.com"));

	internal_LinksTitle = new list;
	internal_LinksUrl = new list;
	internal_LinksSubmenus = new list;

	loadInternalLinks();

	browser_scr_show_attrib.onDataChanged ();


}

brw.onSetVisible (Boolean onoff)
{
	if (onoff)
	{
		if (getPrivateString("Winamp SUI", "UrlXgive", "") != "")
		{
			navurl.setText(getPrivateString("Winamp SUI", "UrlXgive", ""));
			browserReload();
			setPrivateString("Winamp SUI", "UrlXgive", "");
			DebugStrinG("[browser.m] -> External Loading Completed",0);
			return;
		}
		else if (getPrivateString("Winamp SUI", "Browser Open", "") != "")
		{
			navurl.setText(getPrivateString("Winamp SUI", "Browser Open", ""));
			browserReload();
			setPrivateString("Winamp SUI", "Browser Open", "");
			DebugStrinG("[browser.m] -> Loading Completed",0);
			return;
		}
		browserReload();
	}
}


sg.onResize (int x, int y, int w, int h)
{
	if (w > 580)
	{
		g_search.setXmlParam("x", "-267");
		g_search.setXmlParam("w", "263");
		g_navurl.setXmlParam("x", "134");
		g_navurl.setXmlParam("w", "-403");
	}
	else
	{
		g_search.setXmlParam("x", "-167");
		g_search.setXmlParam("w", "163");
		g_navurl.setXmlParam("x", "134");
		g_navurl.setXmlParam("w", "-303");
	}
}

bback.onLeftClick ()
{
	brw.back();
}
bffwd.onLeftClick ()
{
	brw.forward();
}

bhome.onLeftClick ()
{
	navurl.setText(getPrivateString(getSkinName(), "Browser Home", "http://www.winamp.com"));
	browserReload();
}

bhome.onRightButtonUp(int x, int y)
{
	popupmenu editmenu = new popupmenu;
	editmenu.addcommand("Set current Page as default Home",1, 0,0);
	editmenu.addcommand("Set Winamp.com as default Home",2, 0,0);

	int ret = editmenu.popatxy(clientToScreenX(bhome.getLeft()), clientToScreenY(bhome.getTop() + bhome.getHeight()));
	
	if (ret == 1) setPrivateString(getSkinName(), "Browser Home", navurl.getText());
	if (ret == 2) setPrivateString(getSkinName(), "Browser Home", "http://www.winamp.com");

	delete editmenu;
	complete;
}

bbrowse.onLeftClick ()
{
	browserReload();
}

brefresh.onLeftClick ()
{
	browserReload();
}

bstop.onLeftClick ()
{
	brw.stop();
}

// Windows Vista Compatibility hack
browserReload()
{
	string t = navurl.getText();
	//if (isKeyDown(VK_SHIFT)) t = "www." + t + ".com";
	//else if (isKeyDown(VK_CONTROL)) t = "www." + t + ".com";
	brw.navigateUrl(t);
}

navurl.onEnter ()
{
	string t = navurl.getText();
	if (isKeyDown(VK_SHIFT)) t = "www." + t + ".com";
	else if (isKeyDown(VK_CONTROL)) t = "www." + t + ".com";
	brw.navigateUrl(t);
}

brw.onDocumentComplete (String url)
{
	navurl.setText(url);
	//if (browser_scr_show_attrib.getDATA() == "1" && browser_scr_auto_attrib.getData() == "1")
		scr_list.deleteAllItems();
		brw.scrape();
}

brw.onAction (String action, String param, Int x, int y, int p1, int p2, GuiObject source)
{
	if (strlower(action) == "openurl")
	{
		param = prepareWebString(param, "+");
		navurl.setText(param);
		browserReload();
		return;
	}
	if (strlower(action) == "search")
	{
		if (strlower(param) == "go")
		{
			performWebSearch(searchedit.getText());
		}
		else
		{
			searchedit.setText(param);
			performWebSearch(param);
		}
	}
}

// ------------------------------------------------------------------------------
// Web Search
// ------------------------------------------------------------------------------


searchedit.onEnter ()
{
	performWebSearch(getText());
}

performWebSearch (string searchstring)
{
	
	if (strlower(searchstring) == "search")
	{
		return;
	}
	

	if (browser_search_attrib.getData() == "Modern Skins") 
		searchstring = "http://www.winamp.com/skins/search/?s=m&q=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Classic Skins") 
		searchstring = "http://www.winamp.com/skins/search/?s=c&q=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Plugins") 
		searchstring = "http://www.winamp.com/plugins/search/?q=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Videos") 
		searchstring = "http://www.winamp.com/media/video-search/?q=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Music") 
		searchstring = "http://www.winamp.com/media/music-search/?q=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Web Search with Google") 
		searchstring = "http://www.winamp.com/search/web/?q=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "AOL Music Search") 
		searchstring = "http://search.music.aol.com/search/" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "AOL Tickets") 
		searchstring = "http://tickets.aol.com/search.adp?brand=aolboxoffice&query=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Pollstar") 
		searchstring = "http://www.pollstar.com/tour/searchall.pl?By=All&Content=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Truveo Video Search") 
		searchstring = "http://www.truveo.com/?method=truveo.videos.getVideos&query=" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "Bands in Town") 
		searchstring = "http://www.bandsintown.com/" + prepareWebString(searchstring, "");

	else if (browser_search_attrib.getData() == "Live Nation") 
		searchstring = "http://www.livenation.com/search/intermediateSearch/searchstring/" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "JamBase") 
		searchstring = "http://www.jambase.com/search.asp?band=" + prepareWebString(searchstring, "+");

//	else if (browser_search_attrib.getData() == "Podbop") 
//		searchstring = "http://podbop.org/artists/" + prepareWebString(searchstring, "+");

	else if (browser_search_attrib.getData() == "OnTour") 
		searchstring = "http://www.ontour.net/Search.aspx?st=" + prepareWebString(searchstring, "+");

//	else if (browser_search_attrib.getData() == "Eventful") 
//		searchstring = "ERROR -- please get search string! -- " + searchstring;


	navurl.setText(searchstring);
	browserReload();
}

browser_search_attrib.onDataChanged ()
{
/*	if (getData() == "Google Web Search") bsearch.setXmlParam("image", "browser.button.search.google.normal");
	else */if (getData() == "AOL Music Search") 
	{
		bsearch.setXmlParam("image", "browser.button.search.aol.normal");
	}
	else if (getData() == "AOL Tickets") bsearch.setXmlParam("image", "browser.button.search.aol.normal");
//	else if (getData() == "Eventful") bsearch.setXmlParam("image", "browser.button.search.eventful.normal");
	else if (getData() == "Truveo Video Search") bsearch.setXmlParam("image", "browser.button.search.truveo.normal");
	else if (getData() == "Bands in Town") bsearch.setXmlParam("image", "browser.button.search.bit.normal");
	else if (getData() == "JamBase") bsearch.setXmlParam("image", "browser.button.search.jambase.normal");
	else if (getData() == "OnTour") bsearch.setXmlParam("image", "browser.button.search.ontour.normal");
	else if (getData() == "Web Search with Google") bsearch.setXmlParam("image", "browser.button.search.google.normal");
	else if (getData() == "Live Nation") bsearch.setXmlParam("image", "browser.button.search.ln.normal");
	else if (getData() == "Pollstar") bsearch.setXmlParam("image", "browser.button.search.pollstar.normal");
	else bsearch.setXmlParam("image", "browser.button.search.winamp.normal");

	if (brw.isVisible())
	{
		performWebSearch(searchedit.getText());
	}
	
}

bsearch.onLeftClick ()
{
	AttribMenu searchlist = createMenufromAttribList (SearchAttributeList);

	int ret = searchlist.popatxy(clientToScreenX(bsearch.getLeft()), clientToScreenY(bsearch.getTop() + bsearch.getHeight()));
	if (ret >= 0) 
	{
		configAttribute temp = SearchAttributeList.enumItem(ret);
		temp.setData("1");
	}
	complete;
}

createMenufromAttribList (list entries)
{
	Boolean dummy = 0;
	ConfigAttribute temp;
	ConfigItem root = entries.enumItem(0);
	PopUpMenu _menu;
	list submenus, subroots;
	submenus = new list;
	submenus.removeAll();
	subroots = new list;
	subroots.removeAll();
	_menu = new PopUpMenu;
	for ( int i = 1; i < entries.getNumItems(); i++ )
	{
		temp = entries.enumItem(i);
		configItem parent = temp.getParentItem();
		if (parent == root)
		{
			_menu.addCommand (
				temp.getAttributeName(),
				i,
				temp.getData() == "1",
				0
			);		
		}
		else
		{
			popupmenu submenu;
			boolean gotit = FALSE;
			for ( int j = 0; j < submenus.getNumItems(); j++ )
			{
				if (subroots.enumItem(j) == parent)
				{
					submenu = submenus.enumItem(j);
					submenus.removeItem(j);
					gotit = TRUE;
				}
			}
			if (gotit == FALSE)
			{
				subroots.addItem(parent);
				submenu = new popupmenu;
			}
			submenu.addCommand (
				temp.getAttributeName(),
				i,
				temp.getData() == "1",
				0
			);
			submenus.addItem(submenu);
		}
	}
	for ( int i = 0; i < submenus.getNumItems(); i++ )
	{
		//configItem ci = subroots.enumItem(i);
		if (dummy == 1) _menu.addSubMenu(submenus.enumItem(i), "Winamp.com Search"); //TODO ci.getName();
		else
		{
			_menu.addSubMenu(submenus.enumItem(i), "Concert Search"); //TODO ci.getName();
			dummy = 1;
		}
	}
	delete submenus;
	delete subroots;

	return _menu;
}

string prepareWebString (string url, string replace)
{
	string artist = getPlayItemMetaDataString("artist");
	string album = getPlayItemMetaDataString("album");

	if (artist == "") artist = getPlayitemString();
	if (album == "") album = getPlayitemString();

	url = strReplace(url, "%artist%", artist);
	url = strReplace(url, "%album%", album);
	url = strReplace(url, " ", replace);

	return url;
}

string strReplace(string str, string replace, string by)
{
	int pos;
	int len = strlen(replace);
	while (pos = strsearch(str, replace) > -1)
	{
		string str_ = "";
		String _str = "";

		if (pos > 0) str_ = strleft(str, pos);
		
		pos = strlen(str)-pos-len;
		if (pos > 0) _str = strright(str, pos);

		str = str_ + by + _str;
	}
	return str;
}

// ------------------------------------------------------------------------------
// Quick Links
// ------------------------------------------------------------------------------

loadInternalLinks ()
{

	internal_LinksTitle.addItem(">> Report a bug <<");
	internal_LinksUrl.addItem("http://beta.aol.com/projects.php?project=winamp&loc=bugreport");

	internal_LinksSubmenus.addItem("MP3 Music Blogs");
	internal_LinksSubmenus.addItem("Special Offers");

	internal_LinksTitle.addItem("Winamp");
	internal_LinksUrl.addItem("http://www.winamp.com");

	internal_LinksTitle.addItem("SHOUTcast");
	internal_LinksUrl.addItem("http://www.shoutcast.com");

	internal_LinksTitle.addItem("AOL Music");
	internal_LinksUrl.addItem("http://music.aol.com");

	internal_LinksTitle.addItem("Spinner");
	internal_LinksUrl.addItem("http://www.spinner.com/category/mp3-of-the-day/");

	internal_LinksTitle.addItem("[[submenu]1]Spinner");
	internal_LinksUrl.addItem("http://www.spinner.com/category/mp3-of-the-day/");

	internal_LinksTitle.addItem("[[submenu]1]The Hype Machine");
	internal_LinksUrl.addItem("http://www.hypem.com");

	internal_LinksTitle.addItem("[[submenu]1]elbo.ws");
	internal_LinksUrl.addItem("http://elbo.ws/");

	internal_LinksTitle.addItem("[[submenu]1]fluxblog");
	internal_LinksUrl.addItem("http://www.fluxblog.org/");

	internal_LinksTitle.addItem("[[submenu]1]Music For Robots");
	internal_LinksUrl.addItem("http://music.for-robots.com/");

	internal_LinksTitle.addItem("[[submenu]1]Gorillavsbear.net");
	internal_LinksUrl.addItem("http://gorillavsbear.blogspot.com/");

	internal_LinksTitle.addItem("[[submenu]1]Said the Gramphone");
	internal_LinksUrl.addItem("http://www.saidthegramophone.com/");

	internal_LinksTitle.addItem("[[submenu]1]Product Shop NYC");
	internal_LinksUrl.addItem("http://www.productshopnyc.com/htdocs/");

	internal_LinksTitle.addItem("[[submenu]1]brooklynvegan");
	internal_LinksUrl.addItem("http://brooklynvegan.com/");

	internal_LinksTitle.addItem("[[submenu]1]My Old Kentucky Blog");
	internal_LinksUrl.addItem("http://myoldkyhome.blogspot.com/");

	internal_LinksTitle.addItem("[[submenu]1]MP3 4U");
	internal_LinksUrl.addItem("http://mp34u.muzic.com/");

	internal_LinksTitle.addItem("[[submenu]1]3hive");
	internal_LinksUrl.addItem("http://www.3hive.com/");

	internal_LinksTitle.addItem("[[submenu]1]Each Note Secure");
	internal_LinksUrl.addItem("http://www.eachnotesecure.com/");

	internal_LinksTitle.addItem("[[submenu]1]Sonic X");
	internal_LinksUrl.addItem("http://www.sonicx.com/");

	internal_LinksTitle.addItem("[[submenu]1]mfiles");
	internal_LinksUrl.addItem("http://www.mfiles.co.uk/mp3-files.htm");

	internal_LinksTitle.addItem("[[submenu]2]eMusic 50 Free MP3s");
	internal_LinksUrl.addItem("http://www.emusic.com/promo/win/index.html?fref=3D150599");

	internal_LinksTitle.addItem("[[submenu]2]15,000+ Ringtones");
	internal_LinksUrl.addItem("http://offers.thumbplay.com/storefront/212/store.do?storefrontId=3D7&ptr=x=3Dwinamp&iframe=3Dtrue");
}

Function string convertQuickLinkTempName (string name, int subfolder);
string convertQuickLinkTempName (string name, int subfolder)
{
	if (strleft(name, 10) == "[[submenu]")
	{
		name = strright(name, strlen(name) - 10);
		name = getToken(name, "]", 1);
	}
	if (subfolder > 0)
	{
		name = "[[submenu]" + integerToString(subfolder) + "]" + name;
	}
	return name;	
}

Function getSubFolderNum(string name);
int getSubFolderNum (string name)
{
	if (strleft(name, 10) == "[[submenu]")
	{
		name = strright(name, strlen(name) - 10);
		return stringToInteger(getToken(name, "]", 0));
	}
	else
	{
		return 0;
	}	
}

Function getSubFolderNameToNum(string name);
int getSubFolderNameToNum (string name)
{
	int subItems = getPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", 0);

	for ( int i = 1; i <= subItems; i++ )
	{
		if (getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR") == name) return i;
	}
	return -1;
}

Function showQLEdit(int item);
Global Boolean ql_edit_visible;
Global Int editItemNum;
Function PopupMenu createCustomMenu (boolean wantAdd, int startVal);

Global Button edit_ok;
global edit qlname, qlurl;
global dropdownlist qlparent;

showQLEdit (int item)
{
	editItemNum = item;
	ql_edit_visible = 1;
	quicklink_name = newDynamicContainer("browser.quicklink.edit.dialog");
	if (!quicklink_name) return;
	quicklink_name_layout = quicklink_name.getLayout("normal");
	if (!quicklink_name_layout) return;

	qlname = quicklink_name_layout.findObject("edit.name");
	qlurl = quicklink_name_layout.findObject("edit.url");
	qlparent = quicklink_name_layout.findObject("edit.parent");

	qlparent.addItem("* root");
	qlparent.setXmlParam("select", "* root");

	int subItems = getPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", 0);

	for ( int i = 1; i <= subItems; i++ )
	{
		qlparent.addItem(getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"));
		if ( (item + 1000) * (-1) == i ) qlparent.setXmlParam("select", getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"));
	}

	qlparent.addItem("[new subfolder]");

	edit_ok = quicklink_name_layout.findObject("return.values");

	if (item > 0)
	{
		string name = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(item), "ERROR");
		int sf = getSubFolderNum(name);
		if (sf > 0)
		{
			qlparent.setXmlParam("select", getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(sf), "ERROR"));
		}
		qlname.setText(convertQuickLinkTempName(name, 0));
		qlurl.setText(getPrivateString("Winamp Browser QuickLinks", "itemLink_" + integerToString(item), "ERROR"));
	}
	else
	{
		editItemNum += 1000;
		editItemNum *= -1;
		qlurl.setText(navurl.getText());
		editItemNum = 0;
	}

	quicklink_name_layout.center();
	quicklink_name_layout.show();
	quicklink_name_layout.setfocus();
}

edit_ok.onLeftClick ()
{
	if (editItemNum == 0) 
	{
		editItemNum = getPrivateInt("Winamp Browser QuickLinks", "numItems", -1) + 1;
		setPrivateInt("Winamp Browser QuickLinks", "numItems", editItemNum);
	}
	edit e = qlparent.findObject("combobox.edit");
	string sel = e.getText();
	if (sel == "* root")
	{
		setPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(editItemNum), qlname.getText());
	}
	else
	{
		int i = getSubFolderNameToNum (sel);
		if (i > 0)
		{
			setPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(editItemNum), "[[submenu]" + integerToString(i) + "]" + qlname.getText());
		}
		else
		{
			int subitems = getPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", 0);
			setPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(editItemNum), "[[submenu]" + integerToString(subitems + 1) + "]" + qlname.getText());
			
			setPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", subitems + 1);
			setPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(subitems + 1), sel);
		}		
	}
	setPrivateString("Winamp Browser QuickLinks", "itemLink_" + integerToString(editItemNum), qlurl.getText());
	quicklink_name_layout.hide(); 
}


system.onHideLayout (Layout _layout)
{
	if (_layout == quicklink_name_layout) 
	{
		ql_edit_visible = 0;
	}
}


Function showSMEdit(int item);
Global Boolean sm_edit_visible;
Global Int smEditItemNum;
Global Layout submenu_name_layout;
Global Container submenu_name;

Global Button sm_edit_ok;
global edit smname;

showSMEdit (int item)
{
	item += 20000;
	item *= -1;
	smEditItemNum = item;
	sm_edit_visible = 1;
	submenu_name = newDynamicContainer("browser.submenu.edit");
	if (!submenu_name) return;
	submenu_name_layout = submenu_name.getLayout("normal");
	if (!submenu_name_layout) return;

	smname = submenu_name_layout.findObject("edit.name");
	sm_Edit_ok = submenu_name_layout.findObject("return.values");

	if (smEditItemNum)
	{
		smname.setText(getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(smEditItemNum), "ERROR"));
	}

	submenu_name_layout.center();
	submenu_name_layout.show();
	submenu_name_layout.setfocus();
}

sm_edit_ok.onLeftClick ()
{
	if (smEditItemNum == 0) 
	{
		smEditItemNum = getPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", 0);
		smEditItemNum++;
		setPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", smEditItemNum);
	}
	setPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(smEditItemNum), smname.getText());
	submenu_name_layout.hide(); 
}


system.onHideLayout (Layout _layout)
{
	if (_layout == submenu_name_layout) 
	{
		sm_edit_visible = 0;
	}
}


blinks.onLeftClick ()
{
	popupmenu quicklinks = new popupmenu;

	quicklinks = createCustomMenu (1 , 0);

	int ret = quicklinks.popatxy(clientToScreenX(blinks.getLeft()), clientToScreenY(blinks.getTop() + blinks.getHeight()));
	
	delete quicklinks;

	if (ret <= -1000 && ret > -20000) showQLEdit(ret);
	if (ret <= -20000 && ret > -40000) showSMEdit(ret);
	if (ret <= -40000)
	{
		ret += 40000;
		ret *= -1;
		int answer = messageBox ("Do You really want to delete this Subfolder?\nAll Quick Links stored in this folderwill be\ntransfered to the root of the menu!", "Confirmation" , 4, "");
		if (answer = 4)
		{
			int Subitems = getPrivateInt("Winamp Browser QuickLinks Submenus", "numItems", -1);
			int items = getPrivateInt("Winamp Browser QuickLinks", "numItems", -1);

			setPrivateInt("Winamp Browser QuickLinks Submenus", "numItems", Subitems - 1);

			for ( int i = ret; i <= Subitems; i++ )
			{
				string set = getPrivateString("Winamp Browser QuickLinks Submenus", "itemText_" + integerToString(i+1), "ERROR");
				setPrivateString("Winamp Browser QuickLinks Submenus", "itemText_" + integerToString(i), set);
				string vv = "[[submenu]" + integerToString(i) + "]";
				for ( int ii = 1; ii <= items; ii++ )
				{
					string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(ii), "ERROR");
					if (strleft (v, strlen(vv)) == vv)
					{
						v = strright(v, strlen(v) - strlen(vv));
						if (i == ret)
						{								
							setPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(ii), v);
						}
						else
						{
							v = "[[submenu]" + integerToString(i-1) + "]" + v;
							setPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(ii), v);
							
						}
					}						
				}
			}			
		}
		return;
	}

	if (ret <= 0) return;

	int items = getPrivateInt("Winamp Browser QuickLinks", "numItems", -1);

	if (ret > items) 
	{
		if (ret > 10000 && ret < 20000) showQLEdit(ret - 10000);
		else if (ret > 20000 && ret < 30000) 
		{
			ret -= 20000;
			int answer = messageBox ("Do You really want to delete this Quick Link?", "Confirmation" , 4, "");
			if (answer == 4)
			{
				int items = getPrivateInt("Winamp Browser QuickLinks", "numItems", -1);

				setPrivateInt("Winamp Browser QuickLinks", "numItems", items - 1);

				for ( int i = ret; i <= items; i++ )
				{
					string set = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(i+1), "ERROR");
					setPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(i), set);
					set = getPrivateString("Winamp Browser QuickLinks", "itemLink_" + integerToString(i+1), "ERROR");
					setPrivateString("Winamp Browser QuickLinks", "itemLink_" + integerToString(i), set);
				}				
			}
		}
		else if (ret >= 40000)
		{
			navurl.setText (internal_LinksURL.enumItem(ret-40000));
			browserReload();
		}
		return;
	}			
	else if (ret > 0 && ret <= items) 
	{
		navurl.setText (getPrivateString("Winamp Browser QuickLinks", "itemLink_" + integerToString(ret), "ERROR"));
		browserReload();
	}

	complete;
}

createCustomMenu (boolean wantAdd, int startVal)
{
	popupmenu _menu = new popupmenu;

	int subItems = getPrivateInt("Winamp Browser QuickLinks SubMenus", "numItems", 0);
	int items = getPrivateInt("Winamp Browser QuickLinks", "numItems", 0);

	if (wantAdd == 1)
	{
		popupmenu editsub = new popupmenu;
		
		/** Edit Quick links */
		
		popupmenu editmenu = new popupmenu;

		if (subItems > 0)
		{
			for ( int i = 1; i <= subItems; i++ )
			{
				string vv = "[[submenu]" + integerToString(i) + "]";
				popupmenu _sub = new popupmenu;
				for ( int ii = 1; ii <= items; ii++ )
				{
					string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(ii), "ERROR");
					if (strleft (v, strlen(vv)) == vv)
					{
						v = strright(v, strlen(v) - strlen(vv));
						_sub.addCommand (v, 10000 + ii, 0, 0);
					}
				}
				if (_sub.getNumCommands() < 1) _sub.addCommand ("No Quick Links Stored", 0, 0, 1);
				editmenu.AddSubMenu (_sub, getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"));
			}
		}

		for ( int i = 1; i <= items; i++ )
		{
			string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(i), "ERROR");
			if (strleft(v, 10) != "[[submenu]")
			{
				editmenu.addCommand (
					v,
					10000 + i,
					0,
					0
				);
			}
		}

		if (editmenu.getNumCommands() < 1) editmenu.addCommand ("No Quick Links Stored", 0, 0, 1);

		editsub.addSubMenu (editmenu, "Edit Quick Link");

		/** Delete SubMenus */

		popupmenu editmenu2 = new popupmenu;

		if (subItems > 0)
		{
			for ( int i = 1; i <= subItems; i++ )
			{
				popupmenu _sub = new popupmenu;
				for ( int ii = 1; ii <= items; ii++ )
				{
					string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(ii), "ERROR");
					string vv = "[[submenu]" + integerToString(i) + "]";
					if (strleft (v, strlen(vv)) == vv)
					{
						v = strright(v, strlen(v) - strlen(vv));
						_sub.addCommand (v, 20000 + ii, 0, 0);
					}
				}
				if (_sub.getNumCommands() < 1) _sub.addCommand ("No Quick Links Stored", 0, 0, 1);
				editmenu2.AddSubMenu (_sub, getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"));
			}
		}

		for ( int i = 1; i <= items; i++ )
		{
			string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(i), "ERROR");
			if (strleft(v, 10) != "[[submenu]")
			{
				
				editmenu2.addCommand (
					v,
					20000 + i,
					0,
					0
				);
			}
		}

		if (editmenu2.getNumCommands() < 1) editmenu2.addCommand ("No Quick Links Stored", 0, 0, 1);
		editsub.addSubMenu (editmenu2, "Delete Quick Link");
		editsub.addSeparator();

		/** Edit SubMenus */
		
		popupmenu editmenu3 = new popupmenu;

		for ( int i = 1; i <= subItems; i++ )
		{
			editmenu3.addCommand (
				getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"),
				-20000 - i,
				0,
				0
			);
		}
		if (editmenu3.getNumCommands() < 1) editmenu3.addCommand ("No Subfolders Stored", 0, 0, 1);
		editsub.addSubMenu (editmenu3, "Edit Subfolder");


		/** Delete SubMenus */
		
		popupmenu editmenu4 = new popupmenu;

		for ( int i = 1; i <= subItems; i++ )
		{
			editmenu4.addCommand (
				getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"),
				-40000 - i,
				0,
				0
			);
		}
		if (editmenu4.getNumCommands() < 1) editmenu4.addCommand ("No Subfolders Stored", 0, 0, 1);
		editsub.addSubMenu (editmenu4, "Delete Subfolder");

		_menu.addSubMenu (editsub, "Manage Quick Links");
		_menu.addSeparator();

	}

	if (wantAdd == 1)
	{
		_menu.addCommand ("<Add new Subfolder>", -20000, 0, 0);
		_menu.addCommand ("<Add current Page>",	-1000, 0, 0);
		_menu.addSeparator();
	}	

	/*if (items == -1)
	{
		items = 5;
		setPrivateInt("Winamp Browser QuickLinks", "numItems", items);

		setPrivateString("Winamp Browser QuickLinks", "itemText_1", "Winamp");
		setPrivateString("Winamp Browser QuickLinks", "itemLink_1", "http://www.winamp.com");

		setPrivateString("Winamp Browser QuickLinks", "itemText_2", "SHOUTcast");
		setPrivateString("Winamp Browser QuickLinks", "itemLink_2", "http://www.shoutcast.com");

		setPrivateString("Winamp Browser QuickLinks", "itemText_3", "AOL Music");
		setPrivateString("Winamp Browser QuickLinks", "itemLink_3", "http://music.aol.com");

		setPrivateString("Winamp Browser QuickLinks", "itemText_4", "Spinner");
		setPrivateString("Winamp Browser QuickLinks", "itemLink_4", "http://www.spinner.com/category/mp3-of-the-day/");

		setPrivateString("Winamp Browser QuickLinks", "itemText_5", "The Hype Machine");
		setPrivateString("Winamp Browser QuickLinks", "itemLink_5", "http://www.hypem.com");
	}*/

	/** load SubMenus */
	int int_subItems = internal_LinksSubmenus.getNumItems();
	int int_items = internal_LinksTitle.getNumItems();
	for ( int i = 0; i < int_subItems ; i++ )
	{
		popupmenu _sub = new popupmenu;
		for ( int ii = 0; ii < int_items; ii++ )
		{
			string v = internal_LinksTitle.enumItem(ii);
			string vv = "[[submenu]" + integerToString(i+1) + "]";
			if (strleft (v, strlen(vv)) == vv)
			{
				v = strright(v, strlen(v) - strlen(vv));
				_sub.addCommand (v, 40000 + ii, 0, 0);
			}
		}
		if (_sub.getNumCommands() < 1) _sub.addCommand ("No Quick Links Stored", 0, 0, 1);
		_menu.AddSubMenu (_sub, internal_LinksSubmenus.enumItem(i));
	
	}

	if (subItems > 0)
	{		
		for ( int i = 1; i <= subItems; i++ )
		{
			popupmenu _sub = new popupmenu;
			if (wantAdd == 1) _sub.addCommand ("<Add current Page>",	-1000 - i, 0, 0);
			_sub.addSeparator();
			for ( int ii = 1; ii <= items; ii++ )
			{
				string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(ii), "ERROR");
				string vv = "[[submenu]" + integerToString(i) + "]";
				if (strleft (v, strlen(vv)) == vv)
				{
					v = strright(v, strlen(v) - strlen(vv));
					_sub.addCommand (v, startVal + ii, 0, 0);
				}
			}
			if (_sub.getNumCommands() < 3) _sub.addCommand ("No Quick Links Stored", 0, 0, 1);
			_menu.AddSubMenu (_sub, getPrivateString("Winamp Browser QuickLinks SubMenus", "itemText_" + integerToString(i), "ERROR"));
		}
	}
 
	for ( int i = 0; i < int_items; i++ )
	{
		string v = internal_LinksTitle.enumItem(i);
		if (strleft(v, 10) != "[[submenu]")
		{
			_menu.addCommand (
				v,
				40000 + i,
				0,
				0
			);
		}
	}

	for ( int i = 1; i <= items; i++ )
	{
		string v = getPrivateString("Winamp Browser QuickLinks", "itemText_" + integerToString(i), "ERROR");
		if (strleft(v, 10) != "[[submenu]")
		{
			_menu.addCommand (
				v,
				startVal + i,
				0,
				0
			);
		}
	}
	if (_menu.getNumCommands() < 6) _menu.addCommand ("No Quick Links Stored", 0, 0, 1);
	return _menu;
	
}


// ------------------------------------------------------------------------------
// Site Scraper
// ------------------------------------------------------------------------------


scr_open.onLeftClick ()
{
	browser_scr_show_attrib.setData("1");
}

scr_close.onLeftClick ()
{
	browser_scr_show_attrib.setData("0");
}

browser_scr_show_attrib.onDataChanged ()
{
	if (brw == NULL) return;
	if (getData() == "1")
	{
		scr_content.show();
		scr_open.hide();
		scr_open_bg.hide();
		brw.setXmlParam("h", "-142");
		//brw.scrape();
	}
	else
	{
		scr_content.hide();
		scr_open.show();
		scr_open_bg.show();
		brw.setXmlParam("h", "-50");
	}
}

/*
scr_start.onLeftClick ()
{
	scr_start.hide();
	scr_stop.show();
	brw.scrape();
	scr_list.deleteAllItems();
	scr_list.addItem("[DEBUG] brw.startSiteScraper();");
	scr_list.addItem("http://www.skinconsortium.com/vortex.wav");
	//scr_list.addItem("I:\Musik\Sendung mit der Maus - GVL PROTEST.mp3");
}

scr_stop.onLeftClick ()
{
	scr_stop.hide();
	scr_start.show();
//	brw.cancelSiteScraper();
	scr_list.addItem("[DEBUG] brw.cancelSiteScraper();");
}
*/
brw.onMediaLink (string url)
{
	scr_list.addItem(removePath(url));
	scr_list.setSubItem(scr_list.getLastAddedItemPos(), 1, url);
}

Global Boolean prevent = FALSE;

scr_list.onRightClick (int num)
{
	scr_menu = new PopUpMenu;

	scr_menu.addCommand("Play selection\tEnter", 1, 0, 0);
	scr_menu.addCommand("Enqueue selection"/*\tShift+Enter"*/, 2, 0, 0);
	scr_menu.addCommand("Download selection"/*\tShift+Enter"*/, 3, 0, 1);
	scr_menu.addCommand("Send to:"/*\tShift+Enter"*/, 4, 0, 1);

	scr_menu.addSeparator();
	scr_menu.addCommand("Play all"/*\tAlt+Enter"*/, 11, 0, 0);
	scr_menu.addCommand("Enqueue all"/*\tShift+Alt+Enter"*/, 12, 0, 0);
	scr_menu.addCommand("Download all"/*\tShift+Alt+Enter"*/, 13, 0, 1);

	scr_menu.addSeparator();
	scr_menu.addCommand("Select all\tCtrl+A", 101, 0, 0);
	scr_menu.addCommand("Remove selection\tCtrl+R", 201, 0, 0);

	int result = scr_menu.popAtMouse();

	if (result)
	{
		if (result == 1) playListItem(num);
		else if (result == 2) enqueueListItem(num);
		else if (result == 11 )
		{
			boolean enqueue = 0;
			int v = scr_list.getNumItems();
			for ( int i = 0; i < v ; i++ )
			{
				if (enqueue == 0)
				{
					System.playFile(scr_list.getItemLabel(i,1));
					enqueue = 1;
				}
				else
				{
					System.enqueueFile(scr_list.getItemLabel(i,1));
				}		
			}
		}
		else if (result == 12 )
		{
			int v = scr_list.getNumItems();
			for ( int i = 0; i < v ; i++ )
			{
				System.enqueueFile(scr_list.getItemLabel(i,1));		
			}
		}		
		else if (result == 101) selectAll();
		else if (result == 201) 
		{
			if (getFirstItemSelected() == getNextItemSelected(getFirstItemSelected()))
			{
				deletebypos(getFirstItemSelected());
				if (scr_menu) delete scr_menu;
				return;
			}
			
			int v = scr_list.getNumItems();
			for ( int i = v-1; i >= 0 ; i-- )
			{
				if (getItemSelected(i)) deletebypos(i);
			}			
		}
		//deleteByPos(num);
	}

	if (scr_menu) delete scr_menu;
}

scr_download.onLeftClick ()
{
	
}

scr_play.onLeftClick ()
{
	playListItem(num);
}

scr_rescan.onLeftClick ()
{
	scr_list.deleteAllItems();
	brw.scrape();
}

scr_list.onDoubleClick (Int itemnum)
{
	 playListItem(itemnum);
}

scr_list.onKeyDown (int code)
{
	if (code == 13) 
	{
		if (isKeyDown(VK_CONTROL))
		{
			enqueueListItem(scr_list.getItemFocused());
		}
		else
		{
			playListItem(scr_list.getItemFocused());
		}		
		prevent = TRUE;
	}
	else if (code == 65 && isKeyDown(VK_CONTROL))
	{
		selectAll();
		prevent = TRUE;
	}
	else if (code == 82 && isKeyDown(VK_CONTROL))
	{
		if (getFirstItemSelected() == getNextItemSelected(getFirstItemSelected()))
		{
			deletebypos(getFirstItemSelected());
			if (scr_menu) delete scr_menu;
			return;
		}
		
		int v = scr_list.getNumItems();
		for ( int i = v-1; i >= 0 ; i-- )
		{
			if (getItemSelected(i)) deletebypos(i);
		}
	}
	complete;
}

/*scr_list.onKeyDown(int key)
{
	DEBUGstring(integerToString(key), 9);
	
	if (key == "enter") 
	{
		playListItem(scr_list.getItemFocused());	
		prevent = TRUE;
	}
	else if (key == "shift+enter")
	{
		enqueueListItem(scr_list.getItemFocused());
		prevent = TRUE;
	}	
	else if (key == "alt+a")
	{
		scr_list.selectAll();
		prevent = TRUE;
	}
	else if (key == "delete")
	{
		scr_list.deleteByPos(scr_list.getItemFocused());
		prevent = TRUE;
	}
	//complete;
}*/

System.onKeyDown (String key)
{
	if (prevent)
	{
		prevent = FALSE;
		complete;
	}
}


playListItem (int num)
{
	if (scr_list.getFirstItemSelected() == -1)
	{
		
		boolean enqueue = 0;
		int v = scr_list.getNumItems();
		for ( int i = 0; i < v ; i++ )
		{
			if (enqueue == 0)
			{
				System.playFile(scr_list.getItemLabel(i,1));
				enqueue = 1;
			}
			else
			{
				System.enqueueFile(scr_list.getItemLabel(i,1));
			}		
		}
		return;
	}
	

	if (scr_list.getFirstItemSelected() == scr_list.getNextItemSelected(scr_list.getFirstItemSelected()))
	{
		System.playFile(scr_list.getItemLabel(scr_list.getFirstItemSelected(),1));
		return;
	}
	
	boolean enqueue = 0;
	int v = scr_list.getNumItems();
	for ( int i = 0; i < v ; i++ )
	{
		if (scr_list.getItemSelected(i))
		{
			if (enqueue == 0)
			{
				System.playFile(scr_list.getItemLabel(i,1));
				enqueue = 1;
			}
			else
			{
				System.enqueueFile(scr_list.getItemLabel(i,1));
			}
		}		
	}
}

enqueueListItem (int num)
{
	if (scr_list.getFirstItemSelected() == scr_list.getNextItemSelected(scr_list.getFirstItemSelected()))
	{
		System.enqueueFile(scr_list.getItemLabel(scr_list.getFirstItemSelected(),1));
		return;
	}
	
	int v = scr_list.getNumItems();
	for ( int i = 0; i < v ; i++ )
	{
		if (scr_list.getItemSelected(i)) System.enqueueFile(scr_list.getItemLabel(i,1));		
	}
}